import { Component, OnInit, ViewChild }                                       from '@angular/core';
import { ModalHelper }                                                        from '@delon/theme';
import { STColumn, STColumnTag, STComponent, STReq }                          from '@delon/abc';
import { SFDateWidgetSchema, SFSchema, SFSelectWidgetSchema, SFUISchemaItem } from '@delon/form';
import { City }                                                               from "@shared/model/city";
import { map }                                                                from "rxjs/operators";
import { Observable, of }                                                     from "rxjs";
import { CacheService }                                                       from "@delon/cache";
import { UserService }                                                        from "@core/user/user.service";

@Component({
    selector   : 'app-staff-list',
    templateUrl: './list.component.html',
})
export class StaffListComponent implements OnInit
{
    signupLink$: Observable<any>;
    
    url = `api/users/staffs`;
    status: STColumnTag = {
        0: { text: '等待审核', color: '#d08019' },
        1: { text: '审核通过', color: '#24ff9b' },
        2: { text: '已拒绝', color: '#ff2717' },
    };
    @ViewChild('st', { static: false }) st: STComponent;
    columns: STColumn[] = [
        // { title: '编号', index: 'no' },
        // { title: '调用次数', type: 'number', index: 'callNo' },
        { title: '', type: 'img', width: '50px', index: 'imageUrl' },
        // { title: '时间', type: 'date', index: 'updatedAt' },
        { title: '省份', index: 'provinceName' },
        { title: '城市', index: 'cityName' },
        { title: '加入时间', index: 'createdDate', type: 'date' },
        { title: '联系人', index: 'userName' },
        { title: '联系人电话', index: 'phone' },
        { title: '信息介绍', index: 'description' },
        { title: '状态', index: 'status', type: 'tag', tag: this.status, default: '0' },
        {
            title  : '操作',
            buttons: [
                {
                    text  : '通过', type: 'del', popTitle: '确定通过此人员的审核邀请？', click: ( record: any ) =>
                    {
                        this.userService.updateStatus(record.id, 1).subscribe(() => this.st.reload({ sort: 'createdDate,desc' }, { merge: true }))
                    }, iif: item => item.status === 0
                },
                {
                    text  : '拒绝', type: 'del', popTitle: '确定拒绝此人员的审核邀请？', click: ( record: any ) =>
                    {
                        record.status = 1;
                        this.userService.updateStatus(record.id, 2).subscribe(() => this.st.reload({ sort: 'createdDate,desc' }, { merge: true }))
                    }, iif: item => item.status === 0
                },
                {
                    text: '删除', type: 'del', click: record =>
                    {
                        this.userService.delete(record.id).subscribe(() => this.st.reload({ sort: 'createdDate,desc' }, { merge: true }))
                    }
                },
            ]
        }
    ];
    req: STReq = {
        params: { sort: 'createdDate,desc' }
    };
    private pid: any;
    searchSchema: SFSchema = {
        properties: {
            name                 : {
                type : 'string',
                title: '名称',
            },
            'address.province.id': {
                type : 'string',
                title: '省份',
                ui   : {
                    widget   : 'select',
                    width    : 180,
                    asyncData: () => this.cacheService.get<City[]>('api/cities?level=0')
                        .pipe(map(value => value.map(item => ({
                            value: item.id,
                            label: item.name,
                        })))),
                    validator: ( value, formProperty, form ) =>
                    {
                        this.pid = value;
                        if ( value )
                        {
                            form.searchProperty('/address.city.id').resetValue(null, false);
                        }
                    },
                } as SFSelectWidgetSchema
            },
            "address.city.id"    : {
                type : 'string',
                title: '城市',
                ui   : {
                    widget   : 'select',
                    width    : 180,
                    asyncData: () =>
                    {
                        return this.pid ? this.cacheService.get<City[]>(`api/cities?parentId=${ this.pid }`)
                            .pipe(map(value => value.map(i => ({
                                value: i.id,
                                label: i.name,
                            })))) : of([]);
                    },
                }
            },
            createdDate          : {
                type : 'string',
                title: '加入时间',
                ui   : {
                    widget   : 'date',
                    mode     : 'range',
                    showToday: true,
                } as SFDateWidgetSchema,
            }
        },
    };
    ui: SFUISchemaItem = {
        "$address.city.id": {
            widget   : 'select',
            width    : 180,
            asyncData: () =>
            {
                return this.pid ? this.cacheService.get<City[]>(`api/cities?parentId=${ this.pid }`)
                    .pipe(map(value => value.map(i => ({
                        value: i.id,
                        label: i.name,
                    })))) : of([]);
            },
        },
        $name             : {}
    };
    
    constructor( private modal: ModalHelper, private cacheService: CacheService, public userService: UserService )
    {
        this.signupLink$ = userService.http.get(`${ userService.resourceUrl }/requestLink`)
    }
    
    ngOnInit()
    {
    
    }
    
    
    export()
    {
    
    }
}
